/**
 * Project Name:Micro-Services-Common
 * File Name   :TimeUtils.java
 * Package Name:com.micro.services.common.utils
 * Date:2015-9-7上午9:38:32
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
 */

package com.micro.services.common.utils;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.StringUtils;

/*
 * SimpleDateFormat中的日期格式不是同步的。推荐（建议）为每个线程创建独立的格式实例。如果多个线程同时访问一个格式，则它必须保持外部同步。
 * 
 * 类似的线程安全问题的根源,就是要注意 类实例的 成员变量,是否在多线程环境中是否被修改
 * 这种问题多产生于static 对象在多线程中 对其成员变量多次改变值 引起
 */
public class DateUtil
{
	public static final String	PATTERN_yyyy_MM_dd			= "yyyy-MM-dd";
	public static final String	PATTERN_yyyy_MM_dd_HH_mm_ss	= "yyyy-MM-dd HH:mm:ss";
	public static final String	PATTERN_yyyy_MM_dd_HH_mm	= "yyyy-MM-dd HH:mm";

	public static final String	PATTERN_yyyyMMddHHmmssSSS	= "yyyyMMddHHmmssSSS";

	/**
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String format(Date date) throws ParseException {
		return formatDate(date, PATTERN_yyyy_MM_dd_HH_mm);
	}
	public static String format(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		return DateFormatUtils.format(date, pattern);
	}
	public static String formatDate(Date date) throws ParseException {
		return DateFormatUtils.format(date, PATTERN_yyyy_MM_dd);
	}

	public static String formatDate(Date date, String pattern) throws ParseException {
		return DateFormatUtils.format(date, pattern);
	}

	public static Date parseDateTo(String date) throws ParseException {
		return DateUtil.parseDate(date, PATTERN_yyyy_MM_dd);
	}

	public static Date parseDateTo(String date, String pattern) throws ParseException {
		return DateUtil.parseDate(date, pattern);
	}

	public static Date parseDate(String dateString, String pattern) {
		try {
			return DateUtils.parseDate(dateString, new String[] { pattern });
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date addDay(Date date, int day) {
		return DateUtils.addDays(date, day);
	}

	public static String switchToWeekDay(int day) {
		switch (day - 1) {
			case 0:
				return "星期日";

			case 1:
				return "星期一";

			case 2:
				return "星期二";

			case 3:
				return "星期三";

			case 4:
				return "星期四";

			case 5:
				return "星期五";

			case 6:
				return "星期六";

			default:
				return "";
		}
	}

	public static Date timestampToDate(Long timemillis) {
		if (timemillis == null || timemillis == 0) {
			return null;
		}
		return new Date(timemillis * 1000);
	}

	public static Date parseLongToDate(Long time, String format) {
		if (StringUtils.isBlank(format)) {
			format = DateUtil.PATTERN_yyyyMMddHHmmssSSS;
		}
		return DateUtil.parseDate(time.toString(), format);
	}

	public static String timestampToDateString(Long timemillis) {
		if (timemillis == null || timemillis == 0) {
			return null;
		}
		return DateUtil.format(timestampToDate(timemillis), PATTERN_yyyy_MM_dd_HH_mm_ss);
	}
	
	/**
	 * 将时间戳转换为制定格式的日期，timemillis不能为null和0
	 * 
	 * @param timemillis
	 * @param pattern
	 * @return
	 */
	public static String timestampToDateString(Long timemillis, String pattern) {
		if (timemillis == null || timemillis == 0
				|| StringUtils.isEmpty(pattern)) {
			return null;
		}
		return DateUtil.format(timestampToDate(timemillis), pattern);
	}
	
}
